package org.example.controller;

import org.example.model.FormData;
import org.example.service.FormUseDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userData")
@CrossOrigin
public class formDataController {
    @Autowired
    FormUseDataService formUseDataService;
    @PostMapping("/save")
    public int saveData(@RequestBody Map<String, Object> requestDate){
      if(!requestDate.isEmpty()){
          List<FormData> formDatas= formUseDataService.findData(requestDate.get("formName").toString());
          formUseDataService.saveData(new FormData(formDatas.size()+1,requestDate.get("formName").toString(),requestDate.get("formData").toString()));
          return 1;
      }
        return 0;
    }
    @PostMapping("/select")
    public List<FormData> selectData(@RequestBody Map<String, Object> requestDate){
        if(!requestDate.isEmpty()){
            List<FormData> formDatas= formUseDataService.findData(requestDate.get("formName").toString());
            return formDatas;
        }
        return null;
    }

}
